# -*- coding: utf-8 -*-
import pymysql
import time
from .mysql import *
from .user import check_authcode, is_admin


def post_list(uid, authcode, page=1):
    s, data = check_authcode(uid, authcode)
    if not s:
        return False, data
    with mysql(db="BBS") as cur:
        cur.execute(
            """
            SELECT pid, author_id, author_name, title,
            reply_count, ts, last_reply_id, last_reply_name,
            last_reply_ts, is_stuck, is_marked
            FROM posts
            WHERE is_stuck = 1;
            """)
        stuck_posts = cur.fetchall()
        cur.execute(
            """
            SELECT pid, author_id, author_name, title,
            reply_count, ts, last_reply_id, last_reply_name,
            last_reply_ts, is_stuck, is_marked
            FROM posts
            WHERE is_stuck = 0
            LIMIT 20
            OFFSET %s
            ORDER BY last_reply_ts DESC;
            """,
            ((page - 1) * 20, ))
        posts = cur.fetchall()
    return True, {
        "stuck_posts": stuck_post,
        "normal_posts": posts}


def detail(uid, authcode, pid):
    s, data = check_authcode(uid, authcode)
    if not s:
        return False, data
    with mysql(db="BBS") as cur:
        cur.execute(
            """
            SELECT *
            FROM posts
            WHERE pid = %s;
            """,
            (pid, ))
        post = cur.fetchone()
        if not post:
            return False, "pid不存在"
        cur.execute(
            """
            SELECT *
            FROM replies
            WHERE pid = %s
            ORDER BY ts DESC
            LIMIT 50;
            """)
        replies = cur.fetchall()
    return True, {
        "post": post,
        "replies": replies}


def post(uid, authcode, title, content):
    s, data = check_authcode(uid, authcode)
    if not s:
        return False, data
    if not title:
        return False, "标题不能为空"
    if not content:
        return False, "内容不能为空"
    ts = int(time.time())
    with mysql(db="BBS") as cur:
        cur.execute(
            """
            INSERT INTO posts
            VALUES (NULL, %s, %s, %s, %s, 0, %s, %s, %s, %s, 0, 0);
            """,
            (data["uid"], data["screenName"],title, content, ts,
             data["uid"], data["screenName"], ts))
        return True, cur.lastrowid


def stick_post(uid, authcode, pid):
    s, data = check_authcode(uid, authcode)
    if not s:
        return False, data
    if not is_admin(uid):
        return False, "权限不足"
    with mysql(db="BBS") as cur:
        cur.execute(
            """
            SELECT is_stuck
            FROM posts
            WHERE pid = %s;
            """,
            (pid, ))
        is_stuck = cur.fetchone()
        if not is_stuck:
            return False, "pid不存在"
        is_stuck = is_stuck["is_stuck"]
        if is_stuck:
            return False, "该帖已置顶"
        cur.execute(
            """
            UPDATE posts
            SET is_stuck = 1
            WHERE pid = %s;
            """,
            (pid, ))
        return True, pid


def cancel_stick(uid, authcode, pid):
    s, data = check_authcode(uid, authcode)
    if not s:
        return False, data
    if not is_admin(uid):
        return False, "权限不足"
    with mysql(db="BBS") as cur:
        cur.execute(
            """
            SELECT is_stuck
            FROM posts
            WHERE pid = %s;
            """,
            (pid, ))
        is_stuck = cur.fetchone()
        if not is_stuck:
            return False, "pid不存在"
        is_stuck = is_stuck["is_stuck"]
        if not is_stuck:
            return False, "该帖未置顶"
        cur.execute(
            """
            UPDATE posts
            SET is_stuck = 1
            WHERE pid = %s;
            """,
            (pid, ))
        return True, pid


def mark_post(uid, authcode, pid):
    s, data = check_authcode(uid, authcode)
    if not s:
        return False, data
    if not is_admin(uid):
        return False, "权限不足"
    with mysql(db="BBS") as cur:
        cur.execute(
            """
            SELECT is_marked
            FROM posts
            WHERE pid = %s;
            """,
            (pid, ))
        is_marked = cur.fetchone()
        if not is_marked:
            return False, "pid不存在"
        is_marked = is_marked["is_marked"]
        if is_marked:
            return False, "该帖已加精"
        cur.execute(
            """
            UPDATE posts
            SET is_marked = 1
            WHERE pid = %s;
            """,
            (pid, ))
        return True, pid


def cancel_mark(uid, authcode, pid):
    s, data = check_authcode(uid, authcode)
    if not s:
        return False, data
    if not is_admin(uid):
        return False, "权限不足"
    with mysql(db="BBS") as cur:
        cur.execute(
            """
            SELECT is_marked
            FROM posts
            WHERE pid = %s;
            """,
            (pid, ))
        is_marked = cur.fetchone()
        if not is_marked:
            return False, "pid不存在"
        is_marked = is_marked["is_marked"]
        if not is_marked:
            return False, "该帖未加精"
        cur.execute(
            """
            UPDATE posts
            SET is_marked = 0
            WHERE pid = %s;
            """,
            (pid, ))
        return True, pid


def delete_post(uid, authcode, pid):
    s, data = check_authcode(uid, authcode)
    if not s:
        return False, data
    ts = int(time.time())
    with mysql(db="BBS") as cur:
        cur.execute(
            """
            SELECT *
            FROM posts
            WHERE pid = %s;
            """,
            (pid, ))
        pdata = cur.fetchone()
        if not pdata:
            return False, "pid不存在"
        puid = pdata["author_id"]
        if puid != uid:
            if not is_admin(uid):
                return False, "权限不足"
        cur.execute(
            """
            INSERT INTO deleted_posts
            VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
            """,
            (pdata["pid"], pdata["author_id"], pdata["author_name"], pdata["title"],
             pdata["content"], pdata["reply_count"], pdata["ts"], pdata["last_reply_uid"],
             pdata["last_reply_name"], pdata["last_reply_ts"], pdata["is_stuck"],
             pdata["is_marked"], uid, data["screenName"], ts))
        cur.execute(
            """
            DELETE FROM posts
            WHERE pid = %s;
            """,
            (pid, ))
        return True, pid


def delete_post_forever(uid, authcode, pid):
    s, data = check_authcode(uid, authcode)
    if not s:
        return False, data
    ts = int(time.time())
    with mysql(db="BBS") as cur:
        cur.execute(
            """
            SELECT *
            FROM posts
            WHERE pid = %s;
            """,
            (pid, ))
        pdata = cur.fetchone()
        if not pdata:
            return False, "pid不存在"
        puid = pdata["author_id"]
        if puid != uid:
            if not is_admin(uid):
                return False, "权限不足"
        cur.execute(
            """
            DELETE FROM deleted_posts
            WHERE pid = %s;
            """,
            (pid, ))
        cur.execute(
            """
            DELETE FROM replies
            WHERE pid = ?;
            """,
            (pid, ))
        return True, pid


def recover_post(uid, authcode, pid):
    s, data = check_authcode(uid, authcode)
    if not s:
        return False, data
    with mysql(db="BBS") as cur:
        cur.execute(
            """
            SELECT *
            FROM deleted_posts
            WHERE pid = %s;
            """,
            (pid, ))
        pdata = cur.fetchone()
        if not pdata:
            return False, "pid不存在"
        puid = pdata["author_id"]
        if not is_admin(uid):
            if puid == uid:
                if pdata["deleted_by_uid"] != uid:
                    return False, "该帖子已被管理员锁定,请联系管理员恢复"
            else:
                return False, "权限不足"
        cur.execute(
            """
            INSERT INTO posts
            VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
            """,
            (pdata["pid"], pdata["author_id"], pdata["author_name"], pdata["title"],
             pdata["content"], pdata["reply_count"], pdata["ts"], pdata["last_reply_uid"],
             pdata["last_reply_name"], pdata["last_reply_ts"], pdata["is_stuck"],
             pdata["is_marked"]))
        cur.execute(
            """
            DELETE FROM deleted_posts
            WHERE pid = %s;
            """,
            (pid, ))
        return True, pid